<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <el-dialog :title="title" width="40%"  :modal-append-to-body="false">
    <div>
      <span style="color:red;"> *导入说明：</span>
      <p>1.下载的模板,非必要请不要修改，特别是第1，2行</p>
      <p  v-show="ansyEnable">2.支持同步、异步导入。同步：直到执行完毕才返回结果，数据量小，执行时间短可以使用次方法，如果时间长会导致超时异常
        ；异步：没有执行完就会返回，数据量大或者时间长建议使用次方法，执行结果会以消息通知的方式提示</p>
    </div>
    <el-button style="margin:10px 0 10px 0;" type="primary"  icon="el-icon-download" @click="downExcel">下载模版</el-button>
    <div v-show="ansyEnable">
      <avue-switch
              active-color="#13ce66"
              inactive-color="#ff4949"
              v-model="uploadConfig.post.type"
              :dic="uploadConfig.post.dic"
      ></avue-switch>
    </div>


    <el-upload
            ref="uploadrefss"
            :http-request="upAndDowndoad"
            action="#"
            :on-change="onChange"
            :auto-upload="true"
            :show-file-list="true"
            :on-success="importHandleSuccess"
            :on-error="importHandleError"
            accept=".xls, .xlsx"
    >

      <div  style="width:100%;margin-top: 10px">
        <el-button type="primary" icon="el-icon-upload">导入模版</el-button>
      </div>
    </el-upload>
  </el-dialog>
</template>

<script>
  import {importCore,downloadCore} from "@/framework-core/api/core/api";
  import { deepClone } from "@smallwei/avue";
  import { serialize } from '@/framework-core/utils/util'
export default {
  name: "ImportUpload",
  props: {
    baseUrl: {
      required: true
    },
    absoluteUrl:"",
    modelVisible:{},
    modelName: {
      required: false,
      default:""
    },
    ansyEnable: {
      required: false,
      default:false
    },
    params: {},
    callback: {
      type: Function
    }
  },
  data() {
    return {
      title:(this.modelName||"")+ "导入",
      uploadConfig:{headers:{},post:{ "type":0,"dic":[{label: '同步', value: 0}, {label: '异步', value: 1}]}  },
    }
  },
  created() {
    if(this.$refs && this.$refs.uploadrefss){
      this.$refs.uploadrefss.clearFiles();
    }
  },
  inject: ['reload'],
  methods: {
    importHandleSuccess(){
      this.$message.success("importHandleSuccess");
    },
    importHandleError(){
      this.$message.success("importHandleError");
    },
    onChange(){

    },
    downExcel() {
      let url =this.absoluteUrl || downloadCore(this.baseUrl,"");
      url = url + "&fileName=" + encodeURIComponent(this.modelName);
      if(this.params){
        url = url +"&"+ serialize(this.params);
      }
      this.downFile(url, this.modelName);
    },
    upAndDowndoad(ps) {
      let form = new FormData()
      form.append('file', ps.file)
      let paramsObect = {}
      if(this.params) {
        paramsObect = deepClone(this.params);
      }
      paramsObect.fs = this.uploadConfig.post.type;
      importCore(this.baseUrl, form, paramsObect).then(res => {
        let data = res.data
        if (data.state == 200) {
          this.$message.success("导入成功");
          this.loading = false;

          if (this.callBack) {
            this.callBack();
          }else{
            this.reload();
          }
        }
        else if (data.state == -99) {
          if (data.data) {

            if (data.data.url) {
              this.$confirm("导入失败：" + data.message + ',请下载文件查看详情 ?', '提示', {
                confirmButtonText: '下载',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.downFile(data.data.url);
              }).catch(() => {
              })
            } else if (data.data.uuid) {

              this.$confirm("导入失败：" + data.message + ',请下载文件查看详情 ?', '提示', {
                confirmButtonText: '下载',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {

                this.downFile( "api/sys/file/tempFile?f="+data.data.uuid);
              }).catch(() => {
              })


            } else {
              this.$message({
                message: "导入失败：" + data.message||"",
                type: 'error'
              })
            }
          } else {
            this.$message({
              message: "导入失败：" + data.message,
              type: 'error'
            })
          }
        }
        this.loading = false;

      }).catch((data) => {
        if (data.state == 200) {

          this.$message({
            message: "导入失败：" + data.message,
            type: 'error'
          })

          // this.$message.success("导入成功");
          // this.loading = false;
          //
          // if (this.callBack) {
          //   this.callBack();
          // }else{
          //   this.reload();
          // }

        }

      });
    }
  }
}
</script>

<style scoped>
.json-editor {
  height: 100%;
  margin-bottom: 10px;
}

:deep(.json-editor .CodeMirror) {
  font-size: 14px;
  overflow-y: auto;
  font-weight: normal
}

:deep(.json-editor .CodeMirror-scroll) {
}

:deep(.json-editor .cm-s-rubyblue span.cm-string) {
  color: #F08047;
}
</style>
